package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.CheckItem;
import com.xrit.sunnyday.service.CheckItemService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    /**
     * 查询所有检查项(不分页)
     * */
    @GetMapping("findAll")
    public Result findAll(){
        try {
            List<CheckItem> list = checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 添加检查项
     * */
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        checkItemService.add(checkItem);
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 查询所有检查项并判断用户是否使用搜索框查询（分页）
     * */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageInfo<CheckItem> pageInfo = checkItemService.findPage(queryPageBean);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
    }

    /**
     * 根据ID删除检查项
     * */
    @PostMapping("/deleteById")
    public Result deleteById(Integer id){
        checkItemService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 编辑：根据ID查询某项
     * */
    @PostMapping("/selectById")
    public Result selectById(Integer id){
        CheckItem checkItem = checkItemService.selectById(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItem);
    }

    /**
     * 编辑：根据ID修改
     * */
    @PostMapping("/updateById")
    public Result updateById(@RequestBody CheckItem checkItem){
        checkItemService.updateById(checkItem);
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
}
